package com.yanqu.road.dao.impl.union.activitygroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionActivityGroupUnionDaoImpl extends TempDataObjectDao<UnionActivityGroupUnion> {

    public List<UnionActivityGroupUnion> getUnionActivityGroupUnionList(int activityId, List<Integer> groupIdList) {
        List<UnionActivityGroupUnion> list = new ArrayList<>();

        String sql = "select * from `t_s_union_activity_group_union` where `activityId` = ? ";
        if (groupIdList != null && groupIdList.size() > 0) {
            sql += "and groupId in (" + StringUtils.listToString(groupIdList, ",") + ")";
        }

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateUnionActivityGroupUnionSql = "update `t_s_union_activity_group_union` set unionBaseInfo=?,userMap=?,rank=?,locked=?,ability=?,memberMax=? where activityId=? and groupId=? and unionUid=?";

    @Override
    protected boolean updateData(UnionActivityGroupUnion unionActivityGroupUnion) {
        unionActivityGroupUnion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionActivityGroupUnion.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionActivityGroupUnion.getUserMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getLocked()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityGroupUnion.getAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getMemberMax()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityGroupUnion.getUnionUid()));
        if (getDb().executeNoneQuery(updateUnionActivityGroupUnionSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionActivityGroupUnionSql = "insert into `t_s_union_activity_group_union`(activityId,groupId,unionUid,unionBaseInfo,userMap,rank,locked,ability,memberMax) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionActivityGroupUnion unionActivityGroupUnion) {
        unionActivityGroupUnion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityGroupUnion.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionActivityGroupUnion.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionActivityGroupUnion.getUserMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getLocked()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityGroupUnion.getAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityGroupUnion.getMemberMax()));
        if (getDb().executeNoneQuery(insertUnionActivityGroupUnionSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionActivityGroupUnion parseData(ResultSet rs) throws SQLException {
        UnionActivityGroupUnion unionActivityGroupUnion = new UnionActivityGroupUnion();
        unionActivityGroupUnion.setActivityId(rs.getInt("activityId"));
        unionActivityGroupUnion.setGroupId(rs.getInt("groupId"));
        unionActivityGroupUnion.setUnionUid(rs.getString("unionUid"));
        unionActivityGroupUnion.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), new TypeReference<UnionBaseInfo>() {
        }));
        unionActivityGroupUnion.setUserMap(JSON.parseObject(rs.getString("userMap"), new TypeReference<ConcurrentHashMap<Long, UnionActivityGroupUser>>() {
        }));
        unionActivityGroupUnion.setRank(rs.getInt("rank"));
        unionActivityGroupUnion.setLocked(rs.getInt("locked"));
        unionActivityGroupUnion.setMemberMax(rs.getInt("memberMax"));
        unionActivityGroupUnion.setAbility(rs.getString("ability"));
        return unionActivityGroupUnion;
    }

}
